# -*- coding: utf-8 -*-

# @Time : 2020/3/17 11:22
# @Author : WYT
# @Site :
# @File : UploadAccessory.py
# @Software: PyCharm
import datetime
import json

from util.DataQuery.DbTools import DbTools
from util.g import g
from util.selenium.webdriver.common.by import By

from base.BaseMethod import BM
from util.DataQuery.FormDataDecorator import CheckProcess


class UploadAccessory():
    btn_loc = (By.LINK_TEXT, "上传附件")
    current_year = datetime.datetime.now().year
    content_iframe_loc = (By.CSS_SELECTOR, "#innocomFrame")
    btn1 = ".box-body div:nth-of-type(1) div.main"
    btn2 = ".box-body div:nth-of-type(2) div.main"
    btn3 = ".box-body div:nth-of-type(3) div:nth-of-type(2)"
    near_thr_code_loc = (By.XPATH,
                         "//div[@class='box-body']/div[3]/div[contains(string(), '{}')]/div[1]//input".format(current_year - 3))
    near_thr_query_loc = (By.XPATH,
                          "//div[@class='box-body']/div[3]/div[contains(string(), '{}')]/div[1]//button".format(current_year - 3))
    btn4 = ".box-body div:nth-of-type(3) div:nth-of-type(3)"
    near_two_code_loc = (By.XPATH,
                         "//div[@class='box-body']/div[3]/div[contains(string(), '{}')]/div[1]//input".format(current_year - 2))
    near_two_query_loc = (By.XPATH,
                          "//div[@class='box-body']/div[3]/div[contains(string(), '{}')]/div[1]//button".format(current_year - 2))
    btn5 = ".box-body div:nth-of-type(3) div:nth-of-type(4)"
    near_one_code_loc = (By.XPATH,
                         "//div[@class='box-body']/div[3]/div[contains(string(), '{}')]/div[1]//input".format(current_year - 1))
    near_one_query_loc = (By.XPATH,
                          "//div[@class='box-body']/div[3]/div[contains(string(), '{}')]/div[1]//button".format(current_year - 1))
    btn6 = ".box-body div:nth-of-type(4) div.main"
    ps_code_loc = (By.CSS_SELECTOR, ".box-body div:nth-of-type(4) div.main div.title input:nth-of-type(1)")
    ps_query_loc = (By.CSS_SELECTOR, ".box-body div:nth-of-type(4) div.main div.title button")
    btn7 = ".box-body div:nth-of-type(5) div:nth-of-type(2)"
    btn8 = ".box-body div:nth-of-type(5) div:nth-of-type(3)"
    btn9 = ".box-body div:nth-of-type(5) div:nth-of-type(4)"
    btn10 = ".box-body div:nth-of-type(6) div.main"
    near_thr_rdcost_code_loc = (By.CSS_SELECTOR, ".box-body div:nth-of-type(6) div.main div.title input:nth-of-type(1)")
    near_thr_rdcost_query_loc = (By.CSS_SELECTOR, ".box-body div:nth-of-type(6) div.main div.title button")
    btn11 = ".box-body div:nth-of-type(7) div.main"
    tip_str = " div.title"
    btn_str = " a"

    css = By.CSS_SELECTOR

    def __init__(self, driver=None):
        self.driver = driver

    @property
    def class_name(self):
        return self.__class__.__name__

    @CheckProcess
    def main(self):
        try:
            if not g.need_file:
                return
            BM.click(driver=self.driver, ele_loc=self.btn_loc)
            sql = """select creditcode, hp_creditcode, financial_statements 
            from ent_res_agency where co_id={co_id} and year={year}""".format(co_id=g.co_id, year=g.year)
            data = DbTools.get_hitech_record_by_sql(sql=sql)
            print(sql)
            print(data)
            if not data or data.get("creditcode") is None or data.get("hp_creditcode") is None \
                    or data.get("financial_statements") is None:
                raise Exception("上传附件异常,数据不全: {data}".format(data))
            financial_statements = data.get("financial_statements")
            thr_data = json.loads(financial_statements)
            BM.cut_over_frame(driver=self.driver, selector_loc=self.content_iframe_loc)
            BM.click_upload(btn_loc=(self.css, self.btn1 + self.btn_str), tip_loc=(self.css, self.btn1 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.click_upload(btn_loc=(self.css, self.btn2 + self.btn_str), tip_loc=(self.css, self.btn2 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.input(driver=self.driver, ele_loc=self.near_thr_code_loc, keys=thr_data.get("nearThrFSCreditcode"))
            BM.click(driver=self.driver, ele_loc=self.near_thr_query_loc)
            BM.click_upload(btn_loc=(self.css, self.btn3 + self.btn_str), tip_loc=(self.css, self.btn3 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.input(driver=self.driver, ele_loc=self.near_two_code_loc, keys=thr_data.get("nearTwoFSCreditcode"))
            BM.click(driver=self.driver, ele_loc=self.near_two_query_loc)
            BM.click_upload(btn_loc=(self.css, self.btn4 + self.btn_str), tip_loc=(self.css, self.btn4 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.input(driver=self.driver, ele_loc=self.near_one_code_loc, keys=thr_data.get("nearOneFSCreditcode"))
            BM.click(driver=self.driver, ele_loc=self.near_one_query_loc)
            BM.click_upload(btn_loc=(self.css, self.btn5 + self.btn_str), tip_loc=(self.css, self.btn5 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.input(driver=self.driver, ele_loc=self.ps_code_loc, keys=data.get("hp_creditcode"))
            BM.click(driver=self.driver, ele_loc=self.ps_query_loc)
            BM.click_upload(btn_loc=(self.css, self.btn6 + self.btn_str), tip_loc=(self.css, self.btn6 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.click_upload(btn_loc=(self.css, self.btn7 + self.btn_str), tip_loc=(self.css, self.btn7 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.click_upload(btn_loc=(self.css, self.btn8 + self.btn_str), tip_loc=(self.css, self.btn8 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.click_upload(btn_loc=(self.css, self.btn9 + self.btn_str), tip_loc=(self.css, self.btn9 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.input(driver=self.driver, ele_loc=self.near_thr_rdcost_code_loc, keys=data.get("creditcode"))
            BM.click(driver=self.driver, ele_loc=self.near_thr_rdcost_query_loc)
            BM.click_upload(btn_loc=(self.css, self.btn10 + self.btn_str), tip_loc=(self.css, self.btn10 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
            BM.click_upload(btn_loc=(self.css, self.btn11 + self.btn_str), tip_loc=(self.css, self.btn11 + self.tip_str),
                            driver=self.driver, class_name=self.class_name)
        except Exception as err:
            err_msg = "{} file:{} lines:{}".format(err, err.__traceback__.tb_frame.f_globals["__file__"],
                                                           err.__traceback__.tb_lineno)
            raise Exception(err_msg)

if __name__ == '__main__':

    a = UploadAccessory(driver=())
